const formatTime = (date) => {
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const hour = date.getHours();
	const minute = date.getMinutes();
	const second = date.getSeconds();
	return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`;
};

const formatNumber = (n) => {
	n = n.toString();
	return n[1] ? n : `0${n}`;
};

const sxType = (value) => {
	console.log(value)
	let itemList = [ //事项类型
		{
			id: 1,
			name: '求职应聘'
		},
		{
			id: 2,
			name: '业务交流'
		},
		{
			id: 3,
			name: '政务接待'
		},
		{
			id: 4,
			name: '物流货运'
		},
		{
			id: 5,
			name: '参观访问'
		}
	]
	let name = "";
	itemList.forEach(item => {
		if (value == item.id) {
			console.log('ijnlaile')
			name = item.name
		}
	})
	return name;
};

const carType = (value) => {
	let itemList = [{
			id: '1',
			name: '普通轿车'
		},
		{
			id: '2',
			name: '大货物流'
		},
		{
			id: '3',
			name: '设备车辆'
		}
	]
	let name = "其他";
	itemList.forEach(item => {
		if (value == item.id) {
			name = item.name
		}
	})
	return name;
};

const phoneReg = (phone) => {
	let reg = /^1[3456789]\d{9}$/
	let flag = true
	if (!reg.test(phone)) {
		flag = false
	}
	return flag;
}
const idcardReg = (idcard) => {
	let reg = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
	let flag = true
	if (!reg.test(idcard)) {
		flag = false
	}
	return flag;
}

const makeDate = () => {
	let date = '';
	var myDate = new Date();
	let m = myDate.getMonth() < 9 ? ('0' + (myDate.getMonth() + 1)) : myDate.getMonth() + 1;
	console.log(m)
	date = myDate.getFullYear() + '-' + m;
	return date;
};

const posterImg = (base64Data, code, successFn) => {
	let that = this;
	let base64 = base64Data.replace(/^data:image\/\w+;base64,/, ""); //图片替换
	let filePath = wx.env.USER_DATA_PATH + '/qrcode.png';
	uni.getFileSystemManager().writeFile({
		filePath: filePath, //创建一个临时文件名
		data: base64, //写入的文本或二进制数据
		encoding: 'base64', //写入当前文件的字符编码
		success: () => {
			const ctx = uni.createCanvasContext('myCanvas');
			ctx.clearRect(0, 0, 0, 0);
			uni.getImageInfo({
				src: filePath,
				success: function(res) {
					ctx.drawImage('/static/img/poster.png', 0, 0, 192, 234);
					ctx.save();
					ctx.drawImage(filePath, 16, 32, 160, 160);
					ctx.save();
					ctx.setFontSize(14);
					ctx.setFillStyle('red');
					ctx.fillText(code, 100, 215);

					ctx.draw(true, function() {
						uni.canvasToTempFilePath({
							canvasId: 'myCanvas',
							fileType: 'png',
							destWidth: 192,
							destHeight: 234,
							success: function(res) {
								console.log(res);
								uni.hideLoading();
								successFn && successFn(res.tempFilePath);
							},
							fail: function(err) {
								console.log(err);
								uni.hideLoading();
								errFun && errFun(err);
							}
						})
					});
				},
				fail: function(err) {
					console.log(err);
				}
			})
		}
	})
}

module.exports = {
	formatTime,
	sxType,
	carType,
	makeDate,
	phoneReg,
	idcardReg,
	posterImg
};
